home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 9146 / BLP Converter.7z / src / BLP Converter / Program.cs < prev   
Encoding:
Text File  |  2015-05-02  |  3.3 KB  |  88 lines

  1. ∩╗┐namespace BLP_Converter
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.IO;
  6.     using System.Threading.Tasks;
  7.     using System.Windows.Forms;
  8.  
  9.     internal static class Program
  10.     {
  11.         private static string[] nameDB;
  12.  
  13.         private static Dictionary<string, string> fileTypes = new Dictionary<string, string>()
  14.         {
  15.             { "MC", "0x19" }, { "CL", "0x1A" }, { "-C", "0x08" }, { "-H", "0x17" }, { "HL", "0x18" }, { "-P", "0x09" }
  16.         };
  17.  
  18.         public static string[] OpenBLP(string fileName)
  19.         {
  20.             List<string> fileEnts = new List<string>();
  21.             int fileCount = 0;
  22.             try
  23.             {
  24.                 if (nameDB == null)
  25.                 {
  26.                     nameDB = File.ReadAllLines(Path.GetDirectoryName(Application.ExecutablePath) + @"\dlc5lr.dat");
  27.                 }
  28.  
  29.                 MemoryStream ms = new MemoryStream();
  30.                 using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
  31.                 {
  32.                     fs.CopyTo(ms);
  33.                 }
  34.  
  35.                 using (BinaryReader br = new BinaryReader(ms))
  36.                 {
  37.                     br.BaseStream.Position = 0;
  38.                     if (br.ReadInt32() != 0x46495031)
  39.                     {
  40.                         throw new Exception("Unsupported file format!");
  41.                     }
  42.  
  43.                     fileCount = br.ReadInt32();
  44.                     int tableOffset = br.ReadInt32();
  45.                     br.BaseStream.Position = tableOffset;
  46.                     for (int i = 0; i < fileCount; i++)
  47.                     {
  48.                         int nameDBPos = br.ReadInt32();
  49.                         if ((nameDBPos > nameDB.Length - 1) || (nameDBPos < 0))
  50.                         {
  51.                             br.BaseStream.Position += 16;
  52.                             continue;
  53.                         }
  54.  
  55.                         string resolvedName = nameDB[nameDBPos];
  56.                         string fileType = fileTypes[resolvedName.Substring(resolvedName.Length - 2)];
  57.                         int packedSize = br.ReadInt32();
  58.                         int encFlags = br.ReadInt32();
  59.                         int unpackedSize = br.ReadInt32();
  60.                         int checkSum = br.ReadInt32();
  61.                         fileEnts.Add(fileType + "\tchara_dlccos_none\t" + resolvedName + "\t" + packedSize.ToString("X8") + "\t"
  62.                              + encFlags.ToString("X8") + "\t" + unpackedSize.ToString("X8") + "\t" + checkSum.ToString("X8"));
  63.                     }
  64.                 }
  65.  
  66.                 int unresolvedEnts = fileCount - fileEnts.Count;
  67.                 if (unresolvedEnts > 0)
  68.                 {
  69.                     Task.Factory.StartNew(() => { MessageBox.Show("Skipped unknown entries: " + unresolvedEnts.ToString(), "Info"); });
  70.                 }
  71.             }
  72.             catch (Exception e)
  73.             {
  74.                 MessageBox.Show(e.Message, "Error");
  75.             }
  76.  
  77.             return fileEnts.ToArray();
  78.         }
  79.  
  80.         [STAThread]
  81.         private static void Main()
  82.         {
  83.             Application.EnableVisualStyles();
  84.             Application.SetCompatibleTextRenderingDefault(false);
  85.             Application.Run(new MainForm());
  86.         }
  87.     }
  88. }